use dta/SMDLevel.dta, clear

gen SECOND=0
replace SECOND=1 if votes2_TOTAL!=0  

*********************** party running **********************************************
preserve
foreach party in s v_a v a l t kir h_fv h fv oth nsa nkp {
gen runF_`party'=run1_`party'
replace runF_`party'=run2_`party' if SECOND==1
}
keep year SMD run*
sort year SMD
save dta/SMDLevel_Run, replace
restore


*********************** CREATE BLOCS **********************************************
foreach round in 1 2 {
gen vs`round'_LAB=vs`round'_s+vs`round'_nsa+vs`round'_nkp 
gen vs`round'_LIB=vs`round'_a+vs`round'_v_a+vs`round'_v 
gen vs`round'_CON=vs`round'_fv+vs`round'_h_fv+vs`round'_h 
gen vs`round'_OTH=1-vs`round'_LAB-vs`round'_LIB-vs`round'_CON
}

foreach bloc in LAB LIB CON OTH {
gen vsF_`bloc'=vs1_`bloc'
replace vsF_`bloc'=vs2_`bloc' if SECOND==1
}

*** generating leads and lags
xtset SMD year

foreach round in 1 2 F {
	foreach bloc in LAB LIB CON OTH {
	gen next_vs`round'_`bloc'=f3.vs`round'_`bloc'
	gen prev_vs`round'_`bloc'=l3.vs`round'_`bloc'
	}
}

keep year SMD *vs*LAB* *vs*LIB* *vs*CON* *vs*OTH* PR 
order year SMD vs1* vs2* vsF* next_vs1* next_vs2* next_vsF* prev_vs1* prev_vs2* prev_vsF* PR



********************************************************
reshape long vs1_ vs2_ vsF_ next_vs1_ next_vs2_ next_vsF_ prev_vs1_ prev_vs2_ prev_vsF_, i(SMD year) string
rename _j bloc
order year SMD 
********************************************************
gen Reform=0
replace Reform=1 if year>1920


*********************** merge: party running **********************************************
sort year SMD
merge year SMD using dta/SMDLevel_Run
assert _merge==3
drop _merge


***************************************************************************
***************** all three blocs running in all election years************
***************************************************************************

foreach round in 1 2 F{
gen run`round'_LAB=run`round'_s+run`round'_nsa+run`round'_nkp>0
gen run`round'_LIB=run`round'_a+run`round'_v_a+run`round'_v >0
gen run`round'_CON=run`round'_fv+run`round'_h_fv+run`round'_h >0
}

foreach round in 1 2 F{
gen all`round'=0
replace all`round'=1 if (run`round'_LAB+run`round'_LIB+run`round'_CON==3)
}

bysort year: tab1 all*
egen sum_all1=sum(all1),by(SMD bloc)
egen sum_allF=sum(allF),by(SMD bloc)

keep if sum_all1==7 

***************************************************************************
********************** CREATING RANK FOR TOP-TWO **************************
***************************************************************************
egen rank1=rank(vs1_),by(SMD year)
*egen rankF=rank(vsF_),by(SMD year)
*egen NoPF=rowtotal(runF_s-runF_nkp)

gen toptwo=0
replace toptwo=1 if rank1==3|rank1==4
egen test=sum(toptwo),by(SMD year)
assert test==2
replace toptwo=. if year>1920

***************************************************************************
***************************************************************************

foreach year in 1909 1912 1915 1918 1921 1924 1927 {
gen Reform`year'=0
replace Reform`year'=1 if year==`year'
}

foreach year in 1909 1912 1915 1918{
gen zz`year'=toptwo
replace zz`year'=0 if year!=`year'
egen top`year'=max(zz`year'), by(SMD bloc)
gen Reform1921Xtop`year'=Reform1921*top`year'
gen Reform1918Xtop`year'=Reform1918*top`year'
gen Reform1915Xtop`year'=Reform1915*top`year'
gen Reform1912Xtop`year'=Reform1912*top`year'
}

*************** actual Reform
foreach year in 1918 {
eststo clear
xtreg vs1_ Reform1921 Reform1921Xtop`year' if (year==1918|year==1921) & bloc=="LAB", fe i(SMD) cluster(PR)
eststo
estadd local DistrictFE "Yes"
xtreg vs1_ Reform1921 Reform1921Xtop`year' if (year==1918|year==1921) & bloc=="LIB", fe i(SMD) cluster(PR)
eststo
estadd local DistrictFE "Yes"
xtreg vs1_ Reform1921 Reform1921Xtop`year' if (year==1918|year==1921) & bloc=="CON", fe i(SMD) cluster(PR)
eststo
estadd local DistrictFE "Yes"
esttab using tables/TableA5/Reform1921top`year'.tex, replace se b(%9.3f) se(%9.3f) r2 nostar mtitles("Lab." "Lib." "Con.") nonotes prehead("/begin{tabular}{l*{@M}{rr}}" "/hline") posthead(/hline) postfoot("/hline" "/end{tabular}")   scalars("DistrictFE SMD fixed effects") drop(*cons*)
}

*************** actual Reform (no cluster)
foreach year in 1918 {
eststo clear
xtreg vs1_ Reform1921 Reform1921Xtop`year' if (year==1918|year==1921) & bloc=="LAB", fe i(SMD) robust
eststo
estadd local DistrictFE "Yes"
xtreg vs1_ Reform1921 Reform1921Xtop`year' if (year==1918|year==1921) & bloc=="LIB", fe i(SMD) robust
eststo
estadd local DistrictFE "Yes"
xtreg vs1_ Reform1921 Reform1921Xtop`year' if (year==1918|year==1921) & bloc=="CON", fe i(SMD) robust
eststo
estadd local DistrictFE "Yes"
esttab using tables/TableA5/Reform1921top`year'_nocluster.tex, replace se b(%9.3f) se(%9.3f) r2 nostar mtitles("Lab." "Lib." "Con.") nonotes prehead("/begin{tabular}{l*{@M}{rr}}" "/hline") posthead(/hline) postfoot("/hline" "/end{tabular}")   scalars("DistrictFE SMD fixed effects") drop(*cons*)
}



*************** actual Reform control for parties running
foreach year in 1918 {
eststo clear
xtreg vs1_ Reform1921 Reform1921Xtop`year' run1_l if (year==1918|year==1921) & bloc=="LAB", fe i(SMD) cluster(PR)
eststo
estadd local DistrictFE "Yes"
xtreg vs1_ Reform1921 Reform1921Xtop`year' run1_l if (year==1918|year==1921) & bloc=="LIB", fe i(SMD) cluster(PR)
eststo
estadd local DistrictFE "Yes"
xtreg vs1_ Reform1921 Reform1921Xtop`year' run1_l if (year==1918|year==1921) & bloc=="CON", fe i(SMD) cluster(PR)
eststo
estadd local DistrictFE "Yes"
esttab using tables/TableA5/Reform1921top`year'_control.tex, replace se b(%9.3f) se(%9.3f) r2 nostar mtitles("Lab." "Lib." "Con.") coeflabels(run1_l  "Famers' party running")  nonotes prehead("/begin{tabular}{l*{@M}{rr}}" "/hline") posthead(/hline) postfoot("/hline" "/end{tabular}")   scalars("DistrictFE SMD fixed effects") drop(*cons*)
}


*************** actual Reform control for parties running
foreach year in 1918 {
eststo clear
xtreg vs1_ Reform1921 Reform1921Xtop`year' run1_l if (year==1918|year==1921) & bloc=="LAB", fe i(SMD) robust
eststo
estadd local DistrictFE "Yes"
xtreg vs1_ Reform1921 Reform1921Xtop`year' run1_l if (year==1918|year==1921) & bloc=="LIB", fe i(SMD) robust
eststo
estadd local DistrictFE "Yes"
xtreg vs1_ Reform1921 Reform1921Xtop`year' run1_l if (year==1918|year==1921) & bloc=="CON", fe i(SMD) robust
eststo
estadd local DistrictFE "Yes"
esttab using tables/TableA5/Reform1921top`year'_control_nocluster.tex, replace se b(%9.3f) se(%9.3f) r2 nostar mtitles("Lab." "Lib." "Con.") coeflabels(run1_l  "Famers' party running")  nonotes prehead("/begin{tabular}{l*{@M}{rr}}" "/hline") posthead(/hline) postfoot("/hline" "/end{tabular}")   scalars("DistrictFE SMD fixed effects") drop(*cons*)
}
